package com.misic.wed;

import com.alibaba.fastjson.JSONObject;
import com.misic.pojo.Comment;
import com.misic.service.CommentService;
import com.misic.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 评论功能
 *
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping(value = "/add")
    public Object addComment( Comment comment){
        JSONObject jsonObject = new JSONObject();
//        String userId = request.getParameter("userId");           //用户id
//        String type = request.getParameter("type");               //评论类型（0歌曲1歌单）
//        String songId = request.getParameter("songId");           //歌曲id
//        String songListId = request.getParameter("songListId");   //歌单id
//        String content = request.getParameter("content").trim();         //评论内容

        //保存到评论的对象中
//        Comment comment = new Comment();
//        comment.setUserId(Integer.parseInt(userId));
//        comment.setType(new Byte(type));
        if(comment.getType() ==0){
            //Integer.parseInt(songId)
            comment.setSongId(comment.getSongId());
        }else{
            comment.setSongListId(comment.getSongListId());
        }
//        comment.setContent(content);
        boolean flag = commentService.insert(comment);
        if(flag){   //保存成功
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"评论成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"评论失败");
        return jsonObject;
    }

    /**
     * 修改评论
     */
    @PostMapping(value = "/update")
    public Object updateComment( Comment comment){
        JSONObject jsonObject = new JSONObject();
//        String id = request.getParameter("id").trim();                   //主键
//        String userId = request.getParameter("userId").trim();           //用户id
//        String type = request.getParameter("type").trim();               //评论类型（0歌曲1歌单）
//        String songId = request.getParameter("songId").trim();           //歌曲id
//        String songListId = request.getParameter("songListId").trim();   //歌单id
//        String content = request.getParameter("content").trim();         //评论内容
//
//        //保存到评论的对象中
//        Comment comment = new Comment();
//        comment.setId(Integer.parseInt(id));
//        comment.setUserId(Integer.parseInt(userId));
//        comment.setType(new Byte(type));
//        if(comment.getSongId()!=null&&comment.getSongId().equals("")){
////           comment.getSongId() = null;
//        }else {
//            comment.setSongId(Integer.parseInt(songId));
//        }
//        if(!=null&&songListId.equals("")){
//            songListId = null;
//        }else {
//            comment.setSongListId(Integer.parseInt(songListId));
//        }
//        comment.setContent(content);

        boolean flag = commentService.update(comment);
        if(flag){   //保存成功
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"修改失败");
        return jsonObject;
    }

    /**
     * 删除评论
     */
    @GetMapping(value = "/delete")
    public  boolean  deleteComment(@RequestParam("id")Integer id){
//        String id = request.getParameter("id").trim();          //主键
        boolean flag = commentService.delete(id);
        return flag;
    }

    /**
     * 根据主键查询整个对象
     */
    @GetMapping(value = "/selectByPrimaryKey")
    public Comment selectByPrimaryKey(@RequestParam("id")Integer id){
//        String id = request.getParameter("id").trim();          //主键
        return commentService.selectByPrimaryKey(id);
    }

    /**
     * 查询所有评论
     */
    @GetMapping(value = "/allComment")
    public Object allComment(){
        return commentService.allComment();
    }

    /**
     * 查询某个歌曲下的所有评论
     */
    @GetMapping(value = "/commentOfSongId")
    public Object commentOfSongId(@RequestParam("songId")Integer songId){
//        String songId = request.getParameter("songId");          //歌曲id
        return commentService.commentOfSongId(songId);
    }

    /**
     * 查询某个歌单下的所有评论
     */
    @GetMapping(value = "/commentOfSongListId")
    public Object commentOfSongListId(@RequestParam("songListId")Integer songListId){
//        String songListId = request.getParameter("songListId");          //歌曲id
        return commentService.commentOfSongListId(songListId);
    }

    /**
     * 给某个评论点赞
     */
    @PostMapping(value = "/like")
    public JSONObject like(Comment comment){
        JSONObject jsonObject = new JSONObject();
//        String id = request.getParameter("id").trim();           //主键
//        String up = request.getParameter("up").trim();           //用户id
//
//        //保存到评论的对象中
//        Comment comment = new Comment();
//        comment.setId(Integer.parseInt(id));
//        comment.setUp(Integer.parseInt(up));

        boolean flag = commentService.update(comment);
        if(flag){   //保存成功
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"点赞成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"点赞失败");
        return jsonObject;
    }
}
